package com.androidhelper.module.common.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.InflateException;

import com.androidhelper.module.common.app.AppManager;
import com.androidhelper.module.common.handler.IHandler;
import com.androidhelper.module.common.utils.IsEmptyUtils;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * 类的作用 Activity基类
 *
 * @author: lizubing
 */
public abstract class BaseActivity extends SupportActivity implements
        IHandler {

    public Activity mActivity;
    private Unbinder mUnbinder;

  @Nullable
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
     super.onCreate(savedInstanceState);
      AppManager.getInstance().addActivity(this);

     try{

         int layoutResId = attachLayoutRes();
         if(layoutResId!=0) {
             setContentView(layoutResId);
             mUnbinder = ButterKnife.bind(this);
         }

    }catch (Exception e) {
        if (e instanceof InflateException) throw e;
        e.printStackTrace();
    }


     mActivity = this;

    //初始化页面控件
    initView(savedInstanceState);
    //加载数据
    loadData(savedInstanceState);
    setListener();
    initStatusBar();

  }


   @LayoutRes
   protected abstract int attachLayoutRes();

  protected abstract void initView(Bundle savedInstanceState) ;
  protected abstract void loadData(@Nullable Bundle savedInstanceState);
  protected abstract void setListener();
  protected abstract  void initStatusBar();


  @Override
  protected void onDestroy() {
    super.onDestroy();
      if (mUnbinder != null && mUnbinder != Unbinder.EMPTY) {
          mUnbinder.unbind();
      }
      this.mUnbinder = null;

      AppManager.getInstance().removeActivity(this);
  }





    protected boolean isEmpty(Object... o) {
        return IsEmptyUtils.isEmpty(o);
    }








   /* @Override
    public void onBackPressedSupport() {
        // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
        super.onBackPressedSupport();
    }*/


   /*重写父类SupportActivity的onBackPressedSupport()方法，可以轻松的实现按下手机返回键要实现的功能 */

    @Override
    public void onBackPressedSupport() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        } else {
            ActivityCompat.finishAfterTransition(this);
        }
    }



    /**
     * 在Activity中重写onCreateFragmentAnimator()方法来设置所有Fragment的出场消失动画，
     */
    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置默认Fragment动画  默认竖向(和安卓5.0以上的动画相同)
        return super.onCreateFragmentAnimator();
        // 设置横向(和安卓4.x动画相同)
//        return new DefaultHorizontalAnimator();
        // 设置自定义动画
//        return new FragmentAnimator(enter,exit,popEnter,popExit);
    }



}
